package com.logistics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.logistics.compont.ResponseApi;
import com.logistics.pojo.Employee;
import com.logistics.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;

	@GetMapping("/findAll")
	public ResponseApi findAll(){
		List<Employee> list = employeeService.list();
		return ResponseApi.SUCCESS(list);
	}

	@PostMapping("/findEmpByUname")
	public ResponseApi findEmpByUname(String uname){
		QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>()
				.eq("username",uname);
		Employee emp = employeeService.getOne(wrapper);
		return ResponseApi.SUCCESS(emp);
	}

	@PostMapping("/findEmpByUname2")
	public ResponseApi findEmpByUname2(String uname){
		QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>();

		Map<String,Object> map = new HashMap<String,Object>();
		map.put("username",uname);

		wrapper.allEq(map);

		List<Employee> list = employeeService.list(wrapper);

		list.stream().forEach(x->{
			System.out.println(x);
		});

		list.forEach(System.out::print);

		return ResponseApi.SUCCESS(list);
	}

	/**
	 * 返回全部列表
	 * @return
	 */
//	@RequestMapping("/findAll")
//	public List<TbEmployee> findAll(){
//		return employeeService.findAll();
//	}
//
//
//	/**
//	 * 返回全部列表
//	 * @return
//	 */
//	@RequestMapping("/findPage")
//	public PageResult  findPage(int page,int rows){
//		return employeeService.findPage(page, rows);
//	}
//
//	/**
//	 * 增加
//	 * @param employee
//	 * @return
//	 */
//	@RequestMapping("/add")
//	public Result add(@RequestBody TbEmployee employee){
//		try {
//			employeeService.add(employee);
//			return new Result(true, "增加成功");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new Result(false, "增加失败");
//		}
//	}
//
//	/**
//	 * 修改
//	 * @param employee
//	 * @return
//	 */
//	@RequestMapping("/update")
//	public Result update(@RequestBody TbEmployee employee){
//		try {
//			employeeService.update(employee);
//			return new Result(true, "修改成功");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new Result(false, "修改失败");
//		}
//	}
//
//	/**
//	 * 获取实体
//	 * @param id
//	 * @return
//	 */
//	@RequestMapping("/findOne")
//	public TbEmployee findOne(Integer eid){
//		return employeeService.findOne(eid);
//	}
//
//	/**
//	 * 批量删除
//	 * @param ids
//	 * @return
//	 */
//	@RequestMapping("/delete")
//	public Result delete(Integer [] eids){
//		try {
//			employeeService.delete(eids);
//			return new Result(true, "删除成功");
//		} catch (Exception e) {
//			e.printStackTrace();
//			return new Result(false, "删除失败");
//		}
//	}
//
//		/**
//	 * 查询+分页
//	 * @param brand
//	 * @param page
//	 * @param rows
//	 * @return
//	 */
//	@RequestMapping("/search")
//	public PageResult search(@RequestBody TbEmployee employee, int page, int rows  ){
//		return employeeService.findPage(employee, page, rows);
//	}

}
